package com.jkc.xh.fragments;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.adapters.SearchStockAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.models.SearchStockModel;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.network.models.SearchStockResponse;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;
import com.jakewharton.rxbinding.widget.RxTextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.concurrent.TimeUnit;

import retrofit.Call;
import rx.android.schedulers.AndroidSchedulers;

/**
 * 添加自选股票
 */
@EFragment(R.layout.fragment_add_stock)
public class FavStockAddFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnViewClickListener {

    @ViewById(R.id.nav_bar)
    NavBar nav_bar;
    @ViewById(R.id.inputStockCode)
    EditText inputStockCode;
    private SearchStockAdapter mAdapter;

    @Override
    protected void initView(PullRecyclerView recyclerView) {
        RxTextView.afterTextChangeEvents(inputStockCode)
                .debounce(400, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(event -> event.editable())
                .subscribe(text -> enterStockCode(inputStockCode, text));

        mAdapter = new SearchStockAdapter();
        mAdapter.setListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        recyclerView.enableLoadMore(false);
        recyclerView.enableRefresh(false);
        nav_bar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }
        });
    }


    @AfterViews
    void initData() {
        requestData(true);
    }

    private void requestData(final boolean isRefresh) {
    }

    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
//        requestData(false);
        finishLoad(true);
    }

    private Call call;

    @Click(R.id.clearEt)
    void clearInput() {
        inputStockCode.setText("");
    }

    //    @AfterTextChange(R.id.inputStockCode)
    void enterStockCode(TextView tv, Editable text) {
        if (!TextUtils.isEmpty(text)) {
            String code = text.toString();
            try {
                if (call != null) {
                    call.cancel();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            call = TraderManager.searchStock(code, new RestCallBack<SearchStockResponse>() {
                @Override
                public void success(SearchStockResponse response) {
                    mAdapter.replaceWith(response.list);
                    finishLoad(true);
                }

                @Override
                public void failure(RestError error) {

                }
            });

            addJob(call);
        }
    }

    @Override
    public void onItemClick(View view, Object object, int position) {
        SearchStockModel model = (SearchStockModel) object;
        addJob(TraderManager.addFavStock(model.code, new RestCallBack<Object>() {
            @Override
            public void success(Object o) {
                ToastUtil.showToastShort("添加自选成功");
            }

            @Override
            public void failure(RestError error) {

            }
        }));
    }


}
